<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="" />
        <span>后台管理系统</span>
      </div>
      <el-button type="info" @click="logout"> 退出</el-button>
    </el-header>
    <!-- 页面主题区 -->
    <el-container>
      <!-- 侧边栏  动态获取侧边栏的宽度 collapse为true的时候宽度为64PX-->
      <el-aside width="collapse ? '64px' :'200px'" >
        <div class="toggle-button" @click="toggleCollapse"> ||| </div>
        <!-- 侧边栏菜单区 -->
         <!--unique-opened只保持一个子菜单打开 
         collapse 动态绑定布尔值
         collapse-transition 是否折叠动画 
          :router="true"为侧边栏开启路由模式,使用此模式，会在激活导航时以index作为path进行路由跳转
          default-active激活状态，写的是被激活的链接地址-->
        <el-menu
          background-color="#333774"
          text-color="#fff"
          active-text-color="#409fff"
          :unique-opened="true"
          :collapse=" isCollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="activePath"
        >
          <!-- 一级菜单 修改动态index，防止展开一个菜单时都展开-->
          <el-submenu :index="item.id +' ' "  v-for="item in menuList" :key="item.id">
            <!-- 一级菜单模板区 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsList[item.id]"></i>
              <!-- 文本  动态渲染文本-->
              <span>{{item.authName}} </span>
            </template>


            <!-- 二级菜单 -->
            <!-- :index="'/'+items.path": 实现侧边栏的路由链接，链接到path-->
            <el-menu-item  :index="'/'+items.path"  v-for="items in item.children" :key="items.id"
                          @click="saveNavState('/'+items.path)">
              <!-- 二级菜单模板区 -->
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-s-grid"></i>
                <!-- 文本 -->
                <span> {{items.authName}}</span>
                
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主体 -->
      <el-main> 
        <!-- 路由占位符  在此展示welcome的组件-->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data(){
    return {
      //左侧菜单数据
      menuList:[],
      iconsList:{
        '125':'iconfont icon-user ',
        '103': 'iconfont icon-tijikongjian',
        '101':' iconfont icon-shangpin',
        '102': 'iconfont  icon-danju',
        '145': 'iconfont  icon-baobiao'
      },
      //是否折叠
      isCollapse:false,
      //被激活的链接地址
      activePath:''
    }
  },
  created() {
    this.getMenuList(),
    this.activePath=window.sessionStorage.getItem('activePath')//home组件一被创建的时候，就立即将值从sessistorage中取出来赋值给被激活的链接地址
  },
  methods: {
    logout() {
      window.sessionStorage.clear() //清空token
      this.$router.push('/login') //重定向到登录页
    },
    //获取所有菜单
    async getMenuList() {
      const { data: res } = await this.$http.get('menus') //结构data类型，重命名为res
  
     if(res.meta.status!==200) 
     {
       return this.message.error(res.meta.msg)
     } 
     this.menuList=res.data
        console.log(res)//获取data的信息
    },
    //点击按钮实现侧边栏折叠
    toggleCollapse(){
    this.isCollapse=!this.isCollapse
    },
    //保存链接的激活状态
    saveNavState(activePath){
      window.sessionStorage.setItem('activePath',activePath),
      this.activePath=activePath//点击后重新赋值

    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
  widows: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #333774;
  .el-menu{
    border-right: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
.iconfont{
  margin-right: 10px;
}
.toggle-button{
  background-color: #4a5054;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  letter-spacing: 0.2pm;
  text-align: center;
  cursor: pointer;
}
</style>